package com.ivan.stu.notetool.ImageEdit;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RadioButton;

import com.ivan.stu.notetool.R;


/**
 * Created by felix on 2017/12/1 下午2:50.
 */

@SuppressLint("AppCompatCustomView")
public class IMGColorRadio extends RadioButton implements ValueAnimator.AnimatorUpdateListener {

    private static final String TAG = "IMGColorRadio";

    private int mColor = Color.WHITE;

    private int mStrokeColor = Color.TRANSPARENT;

    private float mRadiusRatio = 0f;

    private ValueAnimator mAnimator;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private static final float RADIUS_BASE = 0.6f;

    private static final float RADIUS_RING = 0.9f;

    private static final float RADIUS_BALL = 0.72f;

    private static float mBallRadius  = 0;
    private static  float mRingRadius = 0;
    private int mRadioType;

    public IMGColorRadio(Context context) {
        this(context, null, 0);
    }

    public IMGColorRadio(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs, 0);
    }

    public IMGColorRadio(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs, defStyleAttr);
    }

    private void initialize(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IMGColorRadio);
        mColor = a.getColor(R.styleable.IMGColorRadio_image_color, Color.WHITE);
            mStrokeColor = a.getColor(R.styleable.IMGColorRadio_image_stroke_color, Color.TRANSPARENT);
        mRadiusRatio = a.getFloat(R.styleable.IMGColorRadio_image_size,0);  //比例因子

        mRadioType = a.getInteger(R.styleable.IMGColorRadio_image_type,0);
     //   mRadiusRatio = mRingRadius*0.1f;

        Log.e("mRadiusRatio",mRadiusRatio+"");

        a.recycle();
        setButtonDrawable(null);
        mPaint.setColor(mColor);
        mPaint.setStrokeWidth(4f);
    }

    private ValueAnimator getAnimator() {
        if (mAnimator == null) {
            mAnimator = ValueAnimator.ofFloat(0f, 1f);
            mAnimator.addUpdateListener(this);
            mAnimator.setDuration(200);
            mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        }
        return mAnimator;
    }

    public void setColor(int color) {
        mColor = color;
        mPaint.setColor(mColor);
    }

    public int getColor() {
        return mColor;
    }

    public float getSize(){

        return mBallRadius;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        float hw = getWidth() / 2f, hh = getHeight() / 2f;
        float radius = Math.min(hw, hh);
        canvas.save();

        Log.e("aaaaaaaaa","onDraw");

        //画内圈的圆环
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.FILL);

        if (mRadiusRatio>0.0){

            mBallRadius = getBallRadius(radius)*mRadiusRatio;
         //   Log.e("mBallRadius",mBallRadius+"");

        }else {

            mBallRadius = getBallRadius(radius);
       //     Log.e("mBallRadius",mBallRadius+"");
        }


        canvas.drawCircle(hw, hh, mBallRadius, mPaint);



            if (isChecked()){


                mPaint.setStrokeWidth(4f);
                mStrokeColor = getResources().getColor(R.color.image_color_deep_bule);

            }else {

                if (mRadioType!=1){
                    mPaint.setStrokeWidth(2f);
                    mStrokeColor = Color.WHITE;

                }else {

                    mPaint.setStrokeWidth(2f);
                    mStrokeColor = Color.TRANSPARENT;
                }

            }



        //画外圈的圆环
        mPaint.setColor(mStrokeColor);
        mPaint.setStyle(Paint.Style.STROKE);

        if (mRadiusRatio>0.0){

            mRingRadius = getRingRadius(radius)*mRadiusRatio;

        }else {

            mRingRadius = getRingRadius(radius);
        }


        canvas.drawCircle(hw, hh, mRingRadius, mPaint);
        canvas.restore();
    }

    private float getBallRadius(float radius) {
        return radius * ((RADIUS_BALL - RADIUS_BASE) * mRadiusRatio + RADIUS_BASE);
    }

    private float getRingRadius(float radius) {
        return radius * ((RADIUS_RING - RADIUS_BASE) * mRadiusRatio + RADIUS_BASE);
    }

    @Override
    public void setChecked(boolean checked) {
        boolean isChanged = checked != isChecked();

        super.setChecked(checked);

        if (isChanged) {

            ValueAnimator animator = getAnimator();

            if (checked) {

                mPaint.setStrokeWidth(4f);
                mStrokeColor = getResources().getColor(R.color.image_color_deep_bule);

                invalidate();
               // animator.start();
            } else {

                if (mRadioType!=1){

                    mStrokeColor = Color.WHITE;
                    mPaint.setStrokeWidth(2f);

                }else {

                    mStrokeColor = Color.TRANSPARENT;
                    mPaint.setStrokeWidth(2f);
                }

                invalidate();
              //  animator.reverse();
            }
        }
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
     //   mRadiusRatio = (float) animation.getAnimatedValue();   //取消放大缩小动画。

        invalidate();
    }
}
